/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VesselFrame.java
 *
 * Created on Jun 1, 2011, 4:53:41 PM
 */

package gbbmgmt;

import java.util.*;
import org.jdesktop.application.Action;
import javax.swing.JOptionPane;

/**
 *
 * @author dchapman
 */
public class VesselFrame extends javax.swing.JFrame {

    public ArrayList<String[]> Vessel;

    /** Creates new form VesselFrame */
    public VesselFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBoxVessel = new javax.swing.JComboBox();
        jButtonFirst = new javax.swing.JButton();
        jButtonPrev = new javax.swing.JButton();
        jButtonNext = new javax.swing.JButton();
        jButtonLast = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldFVID = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldFVDesg = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldFVMfg = new javax.swing.JTextField();
        jCheckBoxFVPRM = new javax.swing.JCheckBox();
        jButtonSubNewVessel = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jButtonModVessel = new javax.swing.JButton();
        jButtonVesselAdd = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jTextFieldFVName = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldFVRate = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jTextFieldFVCap = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jComboBoxVessel.setName("jComboBoxVessel"); // NOI18N
        jComboBoxVessel.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBoxVesselPopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getActionMap(VesselFrame.class, this);
        jButtonFirst.setAction(actionMap.get("FirstRecord")); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gbbmgmt.GBBMGMTApp.class).getContext().getResourceMap(VesselFrame.class);
        jButtonFirst.setText(resourceMap.getString("jButtonFirst.text")); // NOI18N
        jButtonFirst.setName("jButtonFirst"); // NOI18N

        jButtonPrev.setAction(actionMap.get("PrevRecord")); // NOI18N
        jButtonPrev.setText(resourceMap.getString("jButtonPrev.text")); // NOI18N
        jButtonPrev.setName("jButtonPrev"); // NOI18N

        jButtonNext.setAction(actionMap.get("NextRecord")); // NOI18N
        jButtonNext.setText(resourceMap.getString("jButtonNext.text")); // NOI18N
        jButtonNext.setName("jButtonNext"); // NOI18N

        jButtonLast.setAction(actionMap.get("LastRecord")); // NOI18N
        jButtonLast.setText(resourceMap.getString("jButtonLast.text")); // NOI18N
        jButtonLast.setName("jButtonLast"); // NOI18N

        jSeparator1.setName("jSeparator1"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jTextFieldFVID.setEditable(false);
        jTextFieldFVID.setName("jTextFieldFVID"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextFieldFVDesg.setName("jTextFieldFVDesg"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jTextFieldFVMfg.setName("jTextFieldFVMfg"); // NOI18N

        jCheckBoxFVPRM.setText(resourceMap.getString("jCheckBoxFVPRM.text")); // NOI18N
        jCheckBoxFVPRM.setName("jCheckBoxFVPRM"); // NOI18N

        jButtonSubNewVessel.setAction(actionMap.get("SubmitEntry")); // NOI18N
        jButtonSubNewVessel.setText(resourceMap.getString("jButtonSubNewVessel.text")); // NOI18N
        jButtonSubNewVessel.setName("jButtonSubNewVessel"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jButtonModVessel.setAction(actionMap.get("ModifyRecord")); // NOI18N
        jButtonModVessel.setText(resourceMap.getString("jButtonModVessel.text")); // NOI18N
        jButtonModVessel.setName("jButtonModVessel"); // NOI18N

        jButtonVesselAdd.setAction(actionMap.get("NewVessel")); // NOI18N
        jButtonVesselAdd.setText(resourceMap.getString("jButtonVesselAdd.text")); // NOI18N
        jButtonVesselAdd.setName("jButtonVesselAdd"); // NOI18N

        jButtonExit.setAction(actionMap.get("ExitVessel")); // NOI18N
        jButtonExit.setText(resourceMap.getString("jButtonExit.text")); // NOI18N
        jButtonExit.setName("jButtonExit"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextFieldFVName.setName("jTextFieldFVName"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jTextFieldFVRate.setName("jTextFieldFVRate"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jTextFieldFVCap.setName("jTextFieldFVCap"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldFVID, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE)
                            .addComponent(jTextFieldFVDesg, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                            .addComponent(jTextFieldFVRate, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldFVCap, javax.swing.GroupLayout.DEFAULT_SIZE, 58, Short.MAX_VALUE)
                                .addGap(231, 231, 231))
                            .addComponent(jCheckBoxFVPRM, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextFieldFVName, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE))))
                    .addComponent(jTextFieldFVMfg, javax.swing.GroupLayout.DEFAULT_SIZE, 448, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonModVessel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonVesselAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 207, Short.MAX_VALUE)
                .addComponent(jButtonExit)
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(419, Short.MAX_VALUE)
                .addComponent(jButtonSubNewVessel)
                .addContainerGap())
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jComboBoxVessel, 0, 326, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonFirst)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonPrev)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonNext)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonLast)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jTextFieldFVCap, jTextFieldFVDesg, jTextFieldFVRate});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                        .addComponent(jButtonFirst)
                        .addComponent(jButtonPrev)
                        .addComponent(jButtonNext)
                        .addComponent(jButtonLast))
                    .addComponent(jComboBoxVessel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 5, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldFVID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jCheckBoxFVPRM))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldFVDesg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldFVName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldFVMfg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldFVRate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(jTextFieldFVCap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
                .addComponent(jButtonSubNewVessel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonModVessel)
                    .addComponent(jButtonVesselAdd)
                    .addComponent(jButtonExit))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBoxVesselPopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBoxVesselPopupMenuWillBecomeInvisible
        // TODO add your handling code here:
        String Itm = jComboBoxVessel.getSelectedItem().toString();

        for(String s[] : Vessel){
            String tmp = s[1] + " - " + s[2];
            if (tmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetFVData(s[0]);
                DBA.closeDBCon();
            }
        }

        SetData();
}//GEN-LAST:event_jComboBoxVesselPopupMenuWillBecomeInvisible

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VesselFrame().setVisible(true);
            }
        });
    }

    public void LoadList(){
        DBA.openDBCon();
        Vessel = DBA.RunSQLQuery("SELECT FVID,FVDesg,FVName FROM FV");
        DBA.closeDBCon();

        for(String s[] : Vessel){
            jComboBoxVessel.addItem(s[1] + " - " + s[2]);
        }

        FirstRecord();
    }

    @Action
    public void FirstRecord() {
        jComboBoxVessel.setSelectedIndex(0);

        String Itm = jComboBoxVessel.getSelectedItem().toString();

        for(String s[] : Vessel){
            String tmp = s[1] + " - " + s[2];
            if (tmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetFVData(s[0]);
                DBA.closeDBCon();
            }
        }
        SetData();
    }

    public void SetData(){
        jTextFieldFVID.setText(Integer.toString(DBA.FVID));
        jTextFieldFVDesg.setText(DBA.FVDesg);
        jTextFieldFVName.setText(DBA.FVName);
        jTextFieldFVMfg.setText(DBA.FVMfg);
        jTextFieldFVRate.setText(Float.toString(DBA.FVRate));
        jTextFieldFVCap.setText(Float.toString(DBA.FVCap));
        jCheckBoxFVPRM.setSelected(DBA.FVPRM);

        jButtonSubNewVessel.setVisible(false);
    }

    @Action
    public void PrevRecord() {
        if(jComboBoxVessel.getSelectedIndex()>=1){
            jComboBoxVessel.setSelectedIndex(jComboBoxVessel.getSelectedIndex()-1);
        }

        String Itm = jComboBoxVessel.getSelectedItem().toString();

        for(String s[] : Vessel){
            String tmp = s[1] + " - " + s[2];
            if (tmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetFVData(s[0]);
                DBA.closeDBCon();
            }
        }
        SetData();
    }

    @Action
    public void NextRecord() {
        if(jComboBoxVessel.getSelectedIndex() <= (jComboBoxVessel.getItemCount()-2)){
            jComboBoxVessel.setSelectedIndex(jComboBoxVessel.getSelectedIndex()+1);
        }

        String Itm = jComboBoxVessel.getSelectedItem().toString();

        for(String s[] : Vessel){
            String tmp = s[1] + " - " + s[2];
            if (tmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetFVData(s[0]);
                DBA.closeDBCon();
            }
        }
        SetData();
    }

    @Action
    public void LastRecord() {
        jComboBoxVessel.setSelectedIndex(jComboBoxVessel.getItemCount()-1);

        String Itm = jComboBoxVessel.getSelectedItem().toString();

        for(String s[] : Vessel){
            String tmp = s[1] + " - " + s[2];
            if (tmp.equals(Itm)) {
                DBA.openDBCon();
                DBA.GetFVData(s[0]);
                DBA.closeDBCon();
            }
        }
        SetData();
    }

    @Action
    public void ExitVessel() {
        dispose();
    }

    @Action
    public void ModifyRecord() {
        int response = JOptionPane.showConfirmDialog(null,
                "This will modify vessel data, continue?",
                "Warning",JOptionPane.WARNING_MESSAGE);

        if (response == 0){
            //User wants to update database, check for differences.
            DBA.openDBCon();
            String FVID = jTextFieldFVID.getText();

            String query = "UPDATE FV SET FVDesg='" +
                        jTextFieldFVDesg.getText() + "',FVName='" +
                        jTextFieldFVName.getText() + "',FVMfg='" +
                        jTextFieldFVMfg.getText() + "',FVRate='" +
                        jTextFieldFVRate.getText() + "',FVCap='" +
                        jTextFieldFVCap.getText() + "',FVPRM='" +
                        Integer.toString((jCheckBoxFVPRM.isSelected())?1:0) +
                        "' WHERE FVID = '" + FVID + "'";
            DBA.RunSQLUpdateQuery(query);

            DBA.GetFVData(FVID);
            DBA.closeDBCon();

            SetData();
        }
    }

    @Action
    public void NewVessel() {
        //set which buttons should be visible.
        jButtonSubNewVessel.setVisible(true);
        jButtonVesselAdd.setVisible(false);
        jButtonFirst.setVisible(false);
        jButtonLast.setVisible(false);
        jButtonNext.setVisible(false);
        jButtonPrev.setVisible(false);
        jButtonModVessel.setVisible(false);
        jComboBoxVessel.setVisible(false);
        jTextFieldFVID.setVisible(false);

        //reset all text fields.
        jTextFieldFVDesg.setText("");
        jTextFieldFVName.setText("");
        jTextFieldFVMfg.setText("");
        jTextFieldFVRate.setText("");
        jTextFieldFVCap.setText("");
        jCheckBoxFVPRM.setSelected(false);
    }

    @Action
    public void SubmitEntry() {
        String query = "'" +
                jTextFieldFVDesg.getText() + "','" +
                jTextFieldFVName.getText() + "','" +
                jTextFieldFVMfg.getText() + "','" +
                jTextFieldFVRate.getText() + "','" +
                jTextFieldFVCap.getText() + "','" +
                Integer.toString((jCheckBoxFVPRM.isSelected())?1:0) + "'";

        String fields = "FVDesg,FVName,FVMfg,FVRate,FVCap,FVPRM";
        DBA.openDBCon();
        DBA.RunSQLInsertQuery("FV",fields,query);
        DBA.closeDBCon();

        //set which buttons should be visible.
        jButtonSubNewVessel.setVisible(false);
        jButtonVesselAdd.setVisible(true);
        jButtonFirst.setVisible(true);
        jButtonLast.setVisible(true);
        jButtonNext.setVisible(true);
        jButtonPrev.setVisible(true);
        jButtonModVessel.setVisible(true);
        jComboBoxVessel.setVisible(true);
        jTextFieldFVID.setVisible(true);

        jComboBoxVessel.removeAllItems();
        LoadList();
        LastRecord();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonFirst;
    private javax.swing.JButton jButtonLast;
    private javax.swing.JButton jButtonModVessel;
    private javax.swing.JButton jButtonNext;
    private javax.swing.JButton jButtonPrev;
    private javax.swing.JButton jButtonSubNewVessel;
    private javax.swing.JButton jButtonVesselAdd;
    private javax.swing.JCheckBox jCheckBoxFVPRM;
    private javax.swing.JComboBox jComboBoxVessel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldFVCap;
    private javax.swing.JTextField jTextFieldFVDesg;
    private javax.swing.JTextField jTextFieldFVID;
    private javax.swing.JTextField jTextFieldFVMfg;
    private javax.swing.JTextField jTextFieldFVName;
    private javax.swing.JTextField jTextFieldFVRate;
    // End of variables declaration//GEN-END:variables

}
